//
//  Sky72LikeCaddieViewController.m
//  Sky72
//
//  Created by ladaka on 13. 10. 4..
//  Copyright (c) 2013년 ladaka Inc. All rights reserved.
//

#import "Sky72LikeCaddieViewController.h"
#import "Sky72LikeCaddieCell.h"
#import "ActionSheetPicker.h"
#import "CaddieObj.h"

@interface Sky72LikeCaddieViewController ()

@end

@implementation Sky72LikeCaddieViewController
@synthesize arrTableViewList;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    CaddieObj *caddie1 = [[CaddieObj alloc] init];
    caddie1.name = @"한예슬";
    caddie1.like = @"미지정";
    
    CaddieObj *caddie2 = [[CaddieObj alloc] init];
    caddie2.name = @"한지민";
    caddie2.like = @"미지정";
    
    self.arrTableViewList = [[NSMutableArray alloc] init];
     
    NSMutableArray *arrList = [[NSMutableArray alloc] init];
    [arrList addObject:caddie1];
    [arrList addObject:caddie2];
    
    NSMutableDictionary *arrDic = [[NSMutableDictionary alloc] init];
    [arrDic setObject:arrList forKey:@"data"];
    [arrDic setObject:@"예약하기" forKey:@"section"];
    
    [self.arrTableViewList addObject:arrDic];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setBtnAll:nil];
    [self setBtnClassic:nil];
    [self setBtnLake:nil];
    [self setBtnOcean:nil];
    [self setBtnSky:nil];
    [self setTvCaddieListView:nil];
    [super viewDidUnload];
}

- (IBAction)onClickCaddie:(id)sender {
    NSLog(@"%s [tag : %d", __FUNCTION__, [sender tag]);
    
    switch ([sender tag]) {
        case 0:     // 전체
            break;
        case 1:     // 클래식
            break;
        case 2:     // 레이크
            break;
        case 3:     // 오션
            break;
        case 4:     // 하늘
            break;
            
        default:
            break;
    }
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Sky72LikeCaddieCell" owner:self options:nil];
//    UIView *view = [nib objectAtIndex:0];
//    
//    return view;
    return nil;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([arrTableViewList count] > 0) {
        return [[[arrTableViewList objectAtIndex:section] objectForKey:@"data"] count];
    }
    return 0;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    if ([arrTableViewList count] > 0) {
        return [arrTableViewList count];
    }
    
    return 0;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Sky72LikeCaddieCell";
    
    Sky72LikeCaddieCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Sky72LikeCaddieCell" owner:nil options:nil];
        cell = [nib objectAtIndex:0];
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if([self.arrTableViewList count] > 0) {
        NSMutableArray *arrContent = [[self.arrTableViewList objectAtIndex:indexPath.section] objectForKey:@"data"];
        CaddieObj *obj = [arrContent objectAtIndex:indexPath.row];
        cell.lbName.text = obj.name;
        [cell.btnLike setTitle:obj.like forState:UIControlStateNormal];
        
        cell.tag = indexPath.row;
        cell.btnLike.tag = indexPath.row;
        NSLog(@"cell.tag: %d", cell.tag);
        cell.delegate = self;
    }
    
    return cell;
    
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"%s", __FUNCTION__);
}

- (void) setLikeState:(id)selectedValue selectCell:(int)selectCell{
    NSLog(@"%s", __FUNCTION__);
    NSMutableArray *arrContent = [[self.arrTableViewList objectAtIndex:0] objectForKey:@"data"];
    
    CaddieObj *obj = [arrContent objectAtIndex:selectCell];
    obj.like = selectedValue;
}

- (void) showAlert:(int)num {
    UIAlertView *alert = nil;
    int tag = num; //num.intValue;
    NSString *message = nil;
    
    if (tag == ALERT_LIKE) {
        message = @"캐디를 Like로 지정하시겠습니까?";
        alert = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"확인", nil];
    } else if (tag == ALERT_NON) {
        message = @"캐디를 Like로 지정취소 하시겠습니까?";
        alert = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"확인", nil];
    } else if (tag == ALERT_OK) {
        message = @"캐디 정보를 저장하시겠습니까?";
        alert = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"확인", nil];
    }
    alert.tag = tag;
    alert.delegate = self;
    [alert show];
}


#pragma mark - Sky72LikeCaddieCellDelegate
- (void) clickLike:(id)sender {
    UIButton *clickButton = (UIButton *) sender;
    NSLog(@"%d", clickButton.tag);
    ActionStringDoneBlock done = ^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
//        if ([sender respondsToSelector:@selector(setText:)]) {
        //            [sender performSelector:@selector(setText:) withObject:selectedValue];
        [self setLikeState:selectedValue selectCell:clickButton.tag];
        
        [self showAlert:3];
//        }
    };
    ActionStringCancelBlock cancel = ^(ActionSheetStringPicker *picker) {
        NSLog(@"Block Picker Canceled");
    };
    
    NSArray *likeState = [NSArray arrayWithObjects:@"Like 캐디", @"미지정", nil];
    [ActionSheetStringPicker showPickerWithTitle:nil rows:likeState initialSelection:0 doneBlock:done cancelBlock:cancel origin:sender];
}

#pragma mark - ActionSheetCustomPickerDelegate
- (void) configurePickerView:(UIPickerView *)pickerView {
    pickerView.showsSelectionIndicator = NO;
        NSLog(@"%s", __FUNCTION__);
}

- (void) actionSheetPickerDidCancel:(AbstractActionSheetPicker *)actionSheetPicker origin:(id)origin {
    NSLog(@"%s", __FUNCTION__);
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch ([alertView tag]) {
        case ALERT_OK:
             [self.tvCaddieListView reloadData];
            break;
            
        default:
            break;
    }
}
@end
